#include "com_voe_VoEVideoSync.h"
#include "jni_api.h"
#include "OutputParam.h"
#include "common_types.h"
#include "voe_video_sync.h"

/*
 * Class:     com_voe_VoEVideoSync
 * Method:    GetInterface
 * Signature: (J)J
 */
JNIEXPORT jlong JNICALL Java_com_voe_VoEVideoSync_GetInterface
	(JNIEnv* env, jclass cls, jlong p_voiceEngine)
{
	return (jlong)webrtc::VoEVideoSync::GetInterface((webrtc::VoiceEngine*)p_voiceEngine);
}

/*
 * Class:     com_voe_VoEVideoSync
 * Method:    Release
 * Signature: (J)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEVideoSync_Release
	(JNIEnv* env, jclass cls, jlong p)
{
	webrtc::VoEVideoSync* pVoEVideoSync = (webrtc::VoEVideoSync*)p;
	return pVoEVideoSync->Release();
}

/*
 * Class:     com_voe_VoEVideoSync
 * Method:    GetPlayoutBufferSize
 * Signature: (JLcom/voe/common/OutputParam;)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEVideoSync_GetPlayoutBufferSize
	(JNIEnv* env, jclass cls, jlong p, jobject obj)
{
	webrtc::VoEVideoSync* pVoEVideoSync = (webrtc::VoEVideoSync*)p;
	int bufferMs = 0;
	jint result = pVoEVideoSync->GetPlayoutBufferSize(bufferMs);
	if(result == 0)
	{
		JniOutputBuilder job(env);
		OutputParam param(env, obj);
		param.setArray(job.add(bufferMs).getOutput());
	}
	return result;
}

/*
 * Class:     com_voe_VoEVideoSync
 * Method:    SetMinimumPlayoutDelay
 * Signature: (JII)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEVideoSync_SetMinimumPlayoutDelay
	(JNIEnv* env, jclass cls, jlong p, jint channel, jint delayMs)
{
	webrtc::VoEVideoSync* pVoEVideoSync = (webrtc::VoEVideoSync*)p;
	return pVoEVideoSync->SetMinimumPlayoutDelay(channel, delayMs);
}

/*
 * Class:     com_voe_VoEVideoSync
 * Method:    SetInitialPlayoutDelay
 * Signature: (JII)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEVideoSync_SetInitialPlayoutDelay
	(JNIEnv* env, jclass cls, jlong p, jint channel, jint delayMs)
{
	webrtc::VoEVideoSync* pVoEVideoSync = (webrtc::VoEVideoSync*)p;
	return pVoEVideoSync->SetInitialPlayoutDelay(channel, delayMs);
}

/*
 * Class:     com_voe_VoEVideoSync
 * Method:    GetDelayEstimate
 * Signature: (JILcom/voe/common/OutputParam;)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEVideoSync_GetDelayEstimate
  (JNIEnv* env, jclass cls, jlong p, jint channel, jobject obj)
{
	webrtc::VoEVideoSync* pVoEVideoSync = (webrtc::VoEVideoSync*)p;
	int delayMs = 0;
	jint result = pVoEVideoSync->GetDelayEstimate(channel, delayMs);
	if(result == 0)
	{
		JniOutputBuilder job(env);
		OutputParam param(env, obj);
		param.setArray(job.add(delayMs).getOutput());
	}
	return result;
}

/*
 * Class:     com_voe_VoEVideoSync
 * Method:    SetInitTimestamp
 * Signature: (JII)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEVideoSync_SetInitTimestamp
	(JNIEnv* env, jclass cls, jlong p, jint channel, jint timestamp)
{
	webrtc::VoEVideoSync* pVoEVideoSync = (webrtc::VoEVideoSync*)p;
	return pVoEVideoSync->SetInitTimestamp(channel, timestamp);
}

/*
 * Class:     com_voe_VoEVideoSync
 * Method:    SetInitSequenceNumber
 * Signature: (JIS)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEVideoSync_SetInitSequenceNumber
	(JNIEnv* env, jclass cls, jlong p, jint channel, jshort sequenceNumber)
{
	webrtc::VoEVideoSync* pVoEVideoSync = (webrtc::VoEVideoSync*)p;
	return pVoEVideoSync->SetInitSequenceNumber(channel, sequenceNumber);
}

/*
 * Class:     com_voe_VoEVideoSync
 * Method:    GetPlayoutTimestamp
 * Signature: (JILcom/voe/common/OutputParam;)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEVideoSync_GetPlayoutTimestamp
  (JNIEnv* env, jclass cls, jlong p, jint channel, jobject obj)
{
	webrtc::VoEVideoSync* pVoEVideoSync = (webrtc::VoEVideoSync*)p;
	unsigned int timestamp = 0;
	jint result = pVoEVideoSync->GetPlayoutTimestamp(channel, timestamp);
	if(result == 0)
	{
		JniOutputBuilder job(env);
		OutputParam param(env, obj);
		param.setArray(job.add((int)timestamp).getOutput());
	}
	return result;
}
